<!-- 资讯模块 -->
<template>
  <div class="consultWrap">
    <div class="navBar">
      <maside
        :number="number"
        @getAsideType="getAsideType"
        @rewriteEvent="rewriteEvent"
      ></maside>
    </div>
    <div class="rWrap">
      <div v-if="showDTZX">
        <Home @rewriteEvent="rewriteEvent" />
      </div>
      <div v-else>
        <div class="condition">
          <condition :keyword="keyword" @submitForm="submitForm"></condition>
          <div class="reportBtn">
            <el-button
              v-show="!clickReport"
              :disabled="!hasData"
              type="primary"
              @click="handleClickReport"
              >报告导出</el-button
            >
            <el-button v-show="clickReport" @click="handleCancleReport"
              >取消导出</el-button
            >
            <i v-show="downloading" class="el-icon-loading"></i>
          </div>
        </div>
        <searchGroup
          v-show="menuType != '个性推荐'"
          :tabLoading="tabLoading"
          :writeInfo="writeInfo"
          @gotoSearch="gotoSearch"
          @reloadColumn="reloadColumn"
        ></searchGroup>
        <div class="tableWrap">
          <mtable
            :menuType="menuType"
            :tableData="tableData"
            :currentPage="currentPage"
            :loadingTable="loadingTable"
            :clickReport="clickReport"
            :hasData="hasData"
            @getCurrentPage="getCurrentPage"
            @getSort="getSort"
            @exportReport="exportReport"
          ></mtable>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import condition from "./Components/conditionForm.vue";
import mtable from "./Components/tableWrap.vue";
import maside from "./Components/aside.vue";
import searchGroup from "./Components/searchGroup.vue";
import Home from "./Components/home.vue";
export default {
  components: {
    condition,
    mtable,
    searchGroup,
    maside,
    Home,
  },
  data() {
    return {
      userInfo: {},
      number: 1, // 用于刷新自定义专题列表
      loadingTable: false,
      menuType: 1,
      clickReport: false,
      downloading: false,
      checkedConsIdsList: [],
      currentPage: 1,
      pageSize: 10,
      sort: 2,
      tableData: {}, //列表数据
      hasData: true,
      mFormData: {
        type: "",
        inputType: "title",
        searchTxt: "",
        beginDate: "",
        industryLabel: "",
        endDate: new Date(),
      },
      postData: {},
      keyword: "",
      pagetype: "",
      tabLoading: false,
      writeInfo: {}, // 用于组合检索回写数据
      showDTZX: true,
      isSenior: false, // 是否高级检索
      seniorInfo: {},
    };
  },
  created() {
    const userInfo = JSON.parse(localStorage.getItem("homeUserInfo"));
    this.userInfo = userInfo;
    this.keyword = this.$route.query.keyword;
    this.mFormData.searchTxt = this.keyword;
    this.pagetype = this.$route.query.type;
    // this.init()
  },
  methods: {
    init() {
      this.submitForm(this.mFormData);
    },
    //查询列表
    async submitForm(formData) {
      // console.log("formData", formData);
      this.isSenior = false;
      let translateSearchTxt = "";
      if (this.menuType == 3 && this.isChinese(formData.searchTxt)) {
        translateSearchTxt = await this.transLate(formData.searchTxt);
      } else {
        translateSearchTxt = formData.searchTxt;
      }
      this.loadingTable = true;
      this.tableData = {};
      this.mFormData = {
        inputType: formData.inputType,
        searchTxt: formData.searchTxt,
        beginDate: formData.beginDate,
        endDate: formData.endDate,
      };
      this.postData = {
        ipi: this.currentPage,
        ips: this.pageSize,
        type: this.menuType, //资讯类型
        order: this.sort, //列表排序
        userId: this.userInfo.id,
        industryLabel: formData.industryLabel,
        fieldObject: {
          fieldType: formData.inputType,
          text: translateSearchTxt,
        },
        dateObject: {
          gte: this.formatDate(formData.beginDate)
            ? this.formatDate(formData.beginDate)
            : "",
          lte: this.formatDate(formData.endDate)
            ? this.formatDate(formData.endDate)
            : "",
        },
      };
      this.$post(this.$Url.consultModule.consultList, this.postData)
        .then((res) => {
          this.loadingTable = false;
          let data = res.data && res.data.result;
          if (data) {
            this.tableData = res.data;
            this.hasData = res.data.result && res.data.result.length > 0;
          }
        })
        .catch((err) => {
          // console.log(err);
          this.loadingTable = false;
        });
    },
    // 判断是否为中文
    isChinese(str) {
      var regEx = "[\u4e00-\u9fa5]";
      let flg = false;
      if (str.match(regEx)) flg = true;
      return flg;
    },
    transLate(searchTxt) {
      if (searchTxt == "") {
        return "";
      }
      return new Promise((resolve, reject) => {
        this.$post(this.$Url.consultModule.translat, {
          text: searchTxt,
        })
          .then((res) => {
            if (res.status === 200) {
              let list =
                res.data.translation[0].translated[0]["translation list"];
              let text = list[0][0];
              resolve(text);
            }
          })
          .catch((err) => {
            reject();
          });
      });
    },
    handleClickReport() {
      this.clickReport = !this.clickReport;
      // 置空选中ids
    },
    // 导出报告
    exportReport(checkedConsIdsList) {
      if (this.downloading) {
        return;
      }
      this.downloading = true;
      this.$post(
        this.$Url.consultModule.export,
        { ids: checkedConsIdsList },
        { responseType: "arraybuffer" }
      )
        .then((res) => {
          this.downloading = false;
          const data = res;
          const url = window.URL.createObjectURL(
            new Blob([data], { type: "application/msword" })
          );
          const link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute(
            "download",
            `${this.formatDateTime(new Date())}.doc`
          );
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch((err) => {
          // console.log("------> 导出报告服务异常", err);
        });
    },
    // 回写组合检索数据
    rewriteEvent(info) {
      this.$set(info, "t", new Date().getTime());
      setTimeout(() => {
        this.writeInfo = info;
      }, 50);
    },
    // 刷新自定义专题列表
    reloadColumn() {
      this.number++;
    },
    // 查询
    gotoSearch(info, status = true) {
      if (status) {
        this.isSenior = true;
        this.seniorInfo = info;
        this.currentPage = 1;
      }
      this.loadingTable = true;
      this.tabLoading = true;
      let param = {
        ipi: this.currentPage,
        ips: this.pageSize,
        type: this.menuType, //资讯类型
        order: this.sort, //列表排序
      };
      Object.assign(param, info);
      this.$post(this.$Url.consultModule.multiFunctionQueryPage, param)
        .then((res) => {
          this.tabLoading = false;
          this.loadingTable = false;
          let data = res.data && res.data.result;
          if (data) {
            this.tableData = res.data;
            this.hasData = res.data.result && res.data.result.length > 0;
          }
        })
        .catch((err) => {
          this.tabLoading = false;
          this.loadingTable = false;
        });
    },

    handleCancleReport() {
      this.clickReport = !this.clickReport;
    },

    getCurrentPage(val) {
      this.currentPage = val;
      if (this.isSenior) {
        this.gotoSearch(this.seniorInfo, false);
      } else {
        this.submitForm(this.mFormData);
      }
    },
    getSort(val) {
      this.sort = val ? 1 : 2;
      this.mFormData.order = val ? 1 : 2;
      this.submitForm(this.mFormData);
    },
    getAsideType(item) {
      this.menuType = item.name;
      if (item.id == "dtzx") {
        this.showDTZX = true;
      } else {
        this.showDTZX = false;
        this.submitForm(this.mFormData);
      }
      // 取消报告
      this.clickReport = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.reportBtn{
  .el-button--primary {
    color: #FFF;
    background-color: $mainColor;
    border-color: $mainColor;
}
.el-button:focus, .el-button:hover {
    color: $mainColor;
    border-color: $mainColor;
    background-color: $hoverColor;
}
}

.consultWrap {
  width: 100%;
  height: 100%;
  background: rgba(246, 247, 251, 1);
  display: flex;
  overflow: hidden;
  justify-content: space-around;
  .navBar {
    width: 13%;
    // height: 6rem;
    // background: #ffffff;
    // border-right: 0.01rem solid #e5e5e5;
  }
  .rWrap {
    width: 84%;
    .condition {
      width: 100%;
      height: 0.8rem;
      background: #ffffff;
      border-bottom: 0.01rem solid #e5e5e5;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 0.3rem;
    }
    .tableWrap {
      width: 100%;
      height: 100%;
      // padding: 0 0.3rem 0.3rem;
    }
  }
  .el-icon-key:before {
    color: $mainColor;
  }
}
</style>
